/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.store;

import static fsahoraires.util.DOMUtils.*;

import org.w3c.dom.*;

import fsahoraires.cours.*;
import fsahoraires.cours.Horaire.Jour;
import fsahoraires.programmes.Programme;
import fsahoraires.programmes.Semestre;

public class CurrentExporter {

	public static void save(Programme programme, Document doc) throws Exception {
		Element root = doc.createElement("fsahoraires");
		doc.appendChild(root);

		// cache
		Element cache = doc.createElement("cache");
		root.appendChild(cache);
		for (Cours cours : programme.getManager().getAllCours()) {
			Element c = doc.createElement("cached");
			c.setAttribute("code", cours.getCode());
			cache.appendChild(c);
			c.appendChild(createElementWithText(doc, "intitule", cours
					.getTitre()));
			c.appendChild(createElementWithText(doc, "abreviation", cours
					.getAbreviation()));
			c.appendChild(createElementWithText(doc, "credits", cours
					.getCredits()
					+ ""));
			c.appendChild(createElementWithText(doc, "heures", cours
					.getHeures()
					+ ""));
			c.appendChild(addHoraire(doc, doc.createElement("semestre1"), cours
					.getHoraire(1)));
			c.appendChild(addHoraire(doc, doc.createElement("semestre2"), cours
					.getHoraire(2)));
		}

		// programme
		Element prog = doc.createElement("programme");
		root.appendChild(prog);
		for (Semestre semestre : programme) {
			Element s = doc.createElement("semestre");
			s.setAttribute("annee", semestre.getAnnee() + "");
			s.setAttribute("semestre", semestre.getSemestre() + "");
			prog.appendChild(s);
			for (Cours cours : semestre) {
				Element c = doc.createElement("cours");
				c.setAttribute("code", cours.getCode());
				s.appendChild(c);
			}
		}
	}

	/**
	 * Ajoute l'horaire à un élément.
	 * 
	 * @param doc
	 * @param e
	 * @param horaire
	 * @return l'élément e
	 */
	private static Element addHoraire(Document doc, Element e, Horaire horaire) {
		for (Jour jour : Jour.values()) {
			for (int tranche = 0; tranche < Horaire.TRANCHES; tranche++) {
				for (Occupation occupation : horaire.getOccupations(jour,
						tranche)) {
					Element o = doc.createElement("occupation");
					o.setAttribute("jour", jour.name());
					o.setAttribute("tranche", (tranche + 1) + "");
					o.setAttribute("infos", occupation.getInfos());
					e.appendChild(o);
					for (Integer semaine : occupation.getSemaines()) {
						Element s = doc.createElement("semaine");
						s.setAttribute("semaine", semaine + "");
						o.appendChild(s);
					}
					for (String local : occupation.getLocaux()) {
						o
								.appendChild(createElementWithText(doc,
										"local", local));
					}
				}
			}
		}
		return e;
	}

}
